package com.sakura.滑动窗口;

public class Code209_长度最小的子数组 {

    public int minSubArrayLen(int target, int[] nums) {
        int res = Integer.MAX_VALUE;

        int l = 0;
        int r = 0;
        int sum = 0;
        while (r < nums.length) {
            sum += nums[r];
            if (sum >= target) {
                while (l <= r && sum >= target) {
                    res = Math.min(res, r - l + 1);
                    sum -= nums[l++];
                }
            }
            r++;
        }

        return res == Integer.MAX_VALUE ? 0 : res;
    }
}
